package com.galgram.project.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxOrder;
import com.galgram.project.service.IFxOrderService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/order")
public class FxOrderController extends BaseController {
    @Autowired
    private IFxOrderService fxOrderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/getList")
    public TableDataInfo getOrderList(Order order) {
        startPage();
        List<Order> list = fxOrderService.getOrderList(order);
        return getDataTable(list);
    }

    /**
     * 查询已结束订单列表
     */
    @GetMapping("/getFinishedList")
    public TableDataInfo getFinishedOrderList(Order order) {
        startPage();
        List<Order> list = fxOrderService.getFinishedOrderList(order);
        return getDataTable(list);
    }

    /**
     * 付款
     */
    @PutMapping("/pay")
    public AjaxResult payOrder(@RequestBody FxOrder fxOrder) {
        fxOrder.setPayTime(LocalDateTime.now());
        fxOrder.setState("学习中");
        fxOrderService.updateById(fxOrder);
        return success("修改成功");
    }

    /**
     * 评分
     */
    @PutMapping("/point")
    public AjaxResult pointOrder(@RequestBody FxOrder fxOrder) {
        fxOrder.setState("已评价");
        fxOrderService.updateById(fxOrder);
        return success("修改成功");
    }

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('project:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxOrder fxOrder) {
        startPage();
        List<FxOrder> list = fxOrderService.selectFxOrderList(fxOrder);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxOrderService.getById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public TableDataInfo add(@RequestBody FxOrder fxOrder) {
        fxOrder.setOrderTime(LocalDateTime.now());
        Long orderNumber = Long.parseLong(String.format("%012d", (int) (Math.random() * 9000000000L) + 1000000000L));
        fxOrder.setOrderNumber(orderNumber);
        fxOrderService.save(fxOrder);
        List<Order> list = fxOrderService.selectOrderByOrderNumber(orderNumber);
        return getDataTable(list);
    }

    /**
     * 取消订单
     */
    @DeleteMapping("/removeOrder/{id}")
    public AjaxResult removeOrder(@PathVariable Long id) {
        fxOrderService.removeById(id);
        return success("取消成功");
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxOrder fxOrder) {
        fxOrderService.updateById(fxOrder);
        return success("修改成功");
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('project:order:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxOrderService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('project:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxOrder fxOrder) {
        List<FxOrder> list = fxOrderService.selectFxOrderList(fxOrder);
        ExcelUtil<FxOrder> util = new ExcelUtil<FxOrder>(FxOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 导入订单列表
     */
    @PreAuthorize("@ss.hasPermi('project:order:import')")
    @Log(title = "订单", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxOrder> util = new ExcelUtil<FxOrder>(FxOrder.class);
        List<FxOrder> userList = util.importExcel(file.getInputStream());
        fxOrderService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:order:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxOrder> list = new ArrayList<FxOrder>();
        ExcelUtil<FxOrder> util = new ExcelUtil<FxOrder>(FxOrder.class);
        util.exportExcel(response, list, "订单数据");
    }
}
